package com.example.controller;

import com.example.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author およそ神
 * @version JDK 1.8
 */
@RestController
@RequestMapping("/person")
public class PersonConsumerController {

    @GetMapping("/fan")
    public String fan() {
        System.out.println("进入fan");
        return "ok";
    }

    /**
     * 理解：消费者，不应该有service层~
     * RestTemplate .... 供我们直接调用就可以了！ 注册到Spring中
     * (地址：url, 实体：Map ,Class<T> responseType)
     * <p>
     * 提供多种便捷访问远程http服务的方法，简单的Restful服务模板~
     */
    @Autowired
    RestTemplate restTemplate;
    //Ribbon:我们这里的地址，应该是一个变量，通过服务名来访问
    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    //private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-PERSON";

    @GetMapping("/consumer/person/get/{id}")
    public Person getById(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/person/searchById/" + id, Person.class);
    }


    @PostMapping("/consumer/person/add")
    public Boolean add(  Person person) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/person/addPerson" ,person, Boolean.class);
    }

    @GetMapping("/consumer/person/list")
    public List<Person> list() {
        System.out.println("调用8888服务,list接口");
        return restTemplate.getForObject(REST_URL_PREFIX + "/person/searchAll" , List.class);
    }



}
